/**
 * JAVACC DEMO 1.0
 */
package com.apache.portal.contorller.trans;

import com.apache.api.vo.ResultEntity;
import com.apache.api.vo.ResultMsg;
import com.apache.cache.util.Validator;
import com.apache.database.model.Page;
import com.apache.portal.common.PortalPlugin;
import com.apache.portal.common.annotion.BeanFactory;
import com.apache.portal.common.connector.impl.AdapterFactory;
import com.apache.portal.common.restfull.RequestMapping;
import com.apache.portal.common.restfull.ResultFullAnntation;
import com.apache.portal.common.restfull.SupperAction;
import com.apache.portal.common.util.PortalPubFactory;
import com.apache.tools.StrUtil;
import com.apache.uct.common.LoginUser;
import com.apache.uct.common.ToolsUtil;
import com.apache.uct.common.entity.User;
import net.sf.json.JSONObject;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * description:  获取对象信息公共接口实现Action
 *
 * @author dyhou 创建时间：2016-7-7
 */
@SuppressWarnings("serial")
@ResultFullAnntation(name = "transInfoAction", urlPatterns = "/trans/info/*")
public class TransInfoAction extends SupperAction {

    private PortalPlugin netPlugin;

    private PortalPlugin orgPlugin;

    private PortalPlugin uctPlugin;

    private PortalPlugin memberPlugin;

    private PortalPlugin auditPlugin;

    private PortalPlugin dictPlugin;

    private PortalPlugin wcmPlugin;

    public void init() {
        netPlugin = (PortalPlugin) BeanFactory.getInstance().getBeans("net");
        orgPlugin = (PortalPlugin) BeanFactory.getInstance().getBeans("org");
        uctPlugin = (PortalPlugin) BeanFactory.getInstance().getBeans("uct");
        memberPlugin = (PortalPlugin) BeanFactory.getInstance().getBeans("member");
        auditPlugin = (PortalPlugin) BeanFactory.getInstance().getBeans("audit");
        dictPlugin = (PortalPlugin) BeanFactory.getInstance().getBeans("dict");
        wcmPlugin = (PortalPlugin) BeanFactory.getInstance().getBeans("wcm");
    }

    /**
     * TODO 简单描述该方法的实现功能（可选）.
     */
    public void doInvoke(HttpServletRequest request, HttpServletResponse response,
            String methodType) throws Exception {
        String doCode = StrUtil.doNull(request.getParameter("doCode"), "dymicSql");
        Method method = doInvoke(this.getClass(), request, response, methodType, doCode);
        if (null != method) {
            Object obj = method.invoke(this, request, response);
            if (!ToolsUtil.isEmpty(obj)) {
                JSONObject array = JSONObject.fromObject(obj);
                this.outputJson(array.toString(), response, request.getParameter("callback"));
            }
        } else {
            gotoErrorPage(request, response, "请求的Action地址未定义");
        }
    }

    /**
     * 查询标的列表
     */
    @RequestMapping(value = "dymicSql", method = "get")
    protected Object dymicSql(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        Map<String, String> params = getParameterMap(request);
        params.put("doCode", "dymicSql");
        params.put("resultType", StrUtil.doNull(request.getParameter("resultType"), "objInfo"));
        params.put("resultObjType", "obj");
        params = AdapterFactory.getInstance()
                .getReqParamForBefor("setReqParam", params, request, response);//前拦截
        String execptionMsg = String.valueOf(params.get("execptionMsg"));
        if (StrUtil.isNotNull(execptionMsg)) {
            ResultMsg msg = new ResultMsg("F", execptionMsg);
            return msg;
        }
        ResultEntity entity = (ResultEntity) netPlugin.doInvoke(request, response,
                params);// PortalPubFactory.getInstance().getPlugin("net")
        //.doInvoke(request, response, params);
        if (null == entity.getEntity()) {
            log.info("获取失败：" + entity.getMessage());
        } else {
            if (entity.getEntity() instanceof String) {
                ResultMsg msg = new ResultMsg("F", entity.getMessage());
                return msg;
            } else {
                return entity.getEntity();
            }
        }
        return null;
    }

    /**
     * pro单对象实体
     */
    @RequestMapping(value = "proEntity", method = "get")
    protected Object proEntity(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        Map<String, String> params = this.getParameterMap(request);
        ResultEntity entity = (ResultEntity) netPlugin.doInvoke(request, response,
                params);//  PortalPubFactory.getInstance().getPlugin("net")
        //.doInvoke(request, response, params);
        Object obj = new Object();
        if (null == entity.getEntity()) {
            log.info(entity.getMessage());
        } else {
            obj = entity.getEntity();
        }
        return obj;
    }

    /**
     * description: 本类样例方法
     */
    @RequestMapping(value = "memberInfo", method = "get")
    protected Object memberInfo(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        Map<String, String> params = this.getParameterMap(request);
        ResultEntity entity = (ResultEntity) memberPlugin.doInvoke(request, response,
                params);// PortalPubFactory.getInstance().getPlugin("member")
        //.doInvoke(request, response, params);
        Object obj = new Object();
        if (null == entity.getEntity()) {
            log.info(entity.getMessage());
        } else {
            obj = entity.getEntity();
        }
        return obj;
    }

    /**
     * description: 根据主键查询机构信息
     */
    @RequestMapping(value = "orgInfo", method = "get")
    protected Object orgInfo(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        Map<String, String> params = this.getParameterMap(request);
        ResultEntity entity = (ResultEntity) orgPlugin.doInvoke(request, response,
                params);// PortalPubFactory.getInstance().getPlugin("org")
        //.doInvoke(request, response, params);
        Object obj = new Object();
        if (null == entity.getEntity()) {
            log.info(entity.getMessage());
        } else {
            obj = entity.getEntity();
        }
        return obj;
    }

    /**
     * description: 根据主键查询报名信息
     */
    @RequestMapping(value = "applyInfo", method = "get")
    protected Object applyInfo(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        Map<String, String> params = this.getParameterMap(request);
        ResultEntity entity = (ResultEntity) netPlugin.doInvoke(request, response,
                params);// PortalPubFactory.getInstance().getPlugin("net")
        //.doInvoke(request, response, params);
        Object obj = new Object();
        if (null == entity.getEntity()) {
            log.info(entity.getMessage());
        } else {
            obj = entity.getEntity();
        }
        return obj;
    }

    @RequestMapping(value = "dictCateInfo", method = "get")
    protected Object dictCateInfo(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        Map<String, String> params = this.getParameterMap(request);
        ResultEntity entity = (ResultEntity) dictPlugin.doInvoke(request, response,
                params);//  PortalPubFactory.getInstance().getPlugin("dict")
        //.doInvoke(request, response, params);
        Object obj = new Object();
        if (null == entity.getEntity()) {
            log.info(entity.getMessage());
        } else {
            obj = entity.getEntity();
        }
        return obj;
    }

    @RequestMapping(value = "dictItemView", method = "get")
    protected Object dictItemView(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        Map<String, String> params = this.getParameterMap(request);
        ResultEntity entity = (ResultEntity) dictPlugin.doInvoke(request, response,
                params);// PortalPubFactory.getInstance().getPlugin("dict")
        //.doInvoke(request, response, params);
        Object obj = new Object();
        if (null == entity.getEntity()) {
            log.info(entity.getMessage());
        } else {
            obj = entity.getEntity();
        }
        return obj;
    }

    /**
     * description: 本类样例方法
     */
    @RequestMapping(value = "viewLogDetail", method = "get")
    protected Object viewLogDetail(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        Map<String, String> params = this.getParameterMap(request);
        ResultEntity entity = (ResultEntity) uctPlugin.doInvoke(request, response,
                params);// PortalPubFactory.getInstance().getPlugin("uct")
        //.doInvoke(request, response, params);
        Map<String, Object> data = new HashMap<String, Object>();
        if (null == entity.getEntity()) {
            log.info(entity.getMessage());
        } else {
            Map map = (Map) entity.getEntity();
            data.put("item", map.get("item"));
            data.put("total", map.get("total"));
            data.put("rows", map.get("rows"));
        }
        return data;
    }

    /**
     * description: 获取项目信息(pro,pub,info)
     */
    @RequestMapping(value = "proInfo", method = "get")
    protected Object proInfo(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        Map<String, String> params = this.getParameterMap(request);
        ResultEntity entity = (ResultEntity) netPlugin.doInvoke(request, response,
                params);// PortalPubFactory.getInstance().getPlugin("net")
        //.doInvoke(request, response, params);
        Object obj = new Object();
        if (null == entity.getEntity()) {
            log.info(entity.getMessage());
        } else {
            obj = entity.getEntity();
        }
        return obj;
    }

    /**
     * description: 根据ID获取部门信息
     */
    @RequestMapping(value = "deptInfo", method = "get")
    protected Object deptInfo(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        Map<String, String> params = this.getParameterMap(request);
        ResultEntity entity = (ResultEntity) orgPlugin.doInvoke(request, response,
                params);//  PortalPubFactory.getInstance().getPlugin("org")
        //.doInvoke(request, response, params);
        Object obj = new Object();
        if (null == entity.getEntity()) {
            log.info(entity.getMessage());
        } else {
            obj = entity.getEntity();
        }
        return obj;
    }

    /**
     * description: 获取transUser信息
     */
    @RequestMapping(value = "userInfo", method = "get")
    protected Object userInfo(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        Map<String, String> params = this.getParameterMap(request);
        User entity = (User) uctPlugin.doInvoke(request, response,
                params);// PortalPubFactory.getInstance().getPlugin("uct").doInvoke(request, response, params);
        if (null == entity) {
            log.info("获取用户信息失败");
        }
        return entity;
    }

    /**
     * 查询结算业务信息
     */
    @RequestMapping(value = "settleFlowEntity", method = "get")
    protected Object settleFlowEntity(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        Map<String, String> params = this.getParameterMap(request);
        ResultEntity entity = (ResultEntity) netPlugin.doInvoke(request, response,
                params);// PortalPubFactory.getInstance().getPlugin("net")
        //.doInvoke(request, response, params);
        if (null == entity.getEntity()) {
            log.info(entity.getMessage());
        }
        return entity.getEntity();
    }

    /**
     * 查询结算凭单信息
     */
    @RequestMapping(value = "settleBillEntity", method = "get")
    protected Object settleBillEntity(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        Map<String, String> params = this.getParameterMap(request);
        ResultEntity entity = (ResultEntity) netPlugin.doInvoke(request, response,
                params);// PortalPubFactory.getInstance().getPlugin("net")
        //.doInvoke(request, response, params);
        if (null == entity.getEntity()) {
            log.info(entity.getMessage());
        }
        return entity.getEntity();
    }

    @RequestMapping(value = "auditInfo", method = "get")
    protected Object auditInfo(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        Map<String, String> params = this.getParameterMap(request);
        ResultEntity entity = (ResultEntity) auditPlugin.doInvoke(request, response,
                params);// PortalPubFactory.getInstance().getPlugin("audit")
        //.doInvoke(request, response, params);
        Object obj = new Object();
        if (null == entity.getEntity()) {
            log.info(entity.getMessage());
        } else {
            obj = entity.getEntity();
        }
        return obj;
    }

    ///以下稿件栏目相关////////////////////

    /**
     * 查询栏目信息
     */
    @RequestMapping(value = "clview", method = "get")
    protected Object clview(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        Map<String, String> params = this.getParameterMap(request);
        ResultEntity entity = (ResultEntity) wcmPlugin.doInvoke(request, response,
                params);//PortalPubFactory.getInstance().getPlugin("wcm")
        //.doInvoke(request, response, params);
        Object obj = new Object();
        if (null == entity.getEntity()) {
            log.info(entity.getMessage());
        } else {
            obj = entity.getEntity();
        }
        return obj;
    }

    /**
     * 查询稿件信息
     */
    @RequestMapping(value = "mtview", method = "get")
    protected Object mtview(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        Map<String, String> params = this.getParameterMap(request);
        String sysName = request.getParameter("sysName");
        sysName = Validator.getDefaultStr(sysName, "cms");
        params.put("datasource", sysName);
        ResultEntity entity = (ResultEntity) wcmPlugin.doInvoke(request, response,
                params);// PortalPubFactory.getInstance().getPlugin("wcm")
        //.doInvoke(request, response, params);
        Object obj = new Object();
        if (null == entity.getEntity()) {
            log.info(entity.getMessage());
        } else {
            obj = entity.getEntity();
        }
        return obj;
    }

    ///稿件栏目相关操作结束////////////////////

    /**
     * description:  查看凭证信息
     */
    @RequestMapping(value = "transBillInfo", method = "get")
    protected Object transBillInfo(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        Map<String, String> params = this.getParameterMap(request);
        ResultEntity entity = (ResultEntity) netPlugin.doInvoke(request, response,
                params);//  PortalPubFactory.getInstance().getPlugin("net")
        //.doInvoke(request, response, params);
        Object obj = new Object();
        if (null == entity.getEntity()) {
            log.info(entity.getMessage());
        } else {
            obj = entity.getEntity();
        }
        return obj;
    }

    /**
     * 查询成交deal对象
     */
    @RequestMapping(value = "dealInfo", method = "get")
    protected Object dealInfo(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        Map<String, String> params = this.getParameterMap(request);
        ResultEntity entity = (ResultEntity) netPlugin.doInvoke(request, response,
                params);// PortalPubFactory.getInstance().getPlugin("net")
        //.doInvoke(request, response, params);
        if (null == entity.getEntity()) {
            log.info(entity.getMessage());
        }
        return entity.getEntity();
    }

    /**
     * 推送信息到竞价系统
     */
    @RequestMapping(value = "pushBid", method = "get")
    protected Object pushBidInfo(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        LoginUser loginUser = PortalPubFactory.getInstance().getLoginUser(request);
        if (ToolsUtil.isEmpty(loginUser)) {
            gotoLogin(request, response, "");
            return null;
        }
        Map<String, String> params = this.getParameterMap(request);
        ResultEntity entity = (ResultEntity) netPlugin.doInvoke(request, response,
                params);//  PortalPubFactory.getInstance().getPlugin("net")
        //.doInvoke(request, response, params);
        ResultMsg msg = new ResultMsg("T", "推送成功");
        if (Validator.isEmpty(entity.getEntity())) {
            msg = new ResultMsg("F", entity.getMessage());
        }
        return msg;
    }

    @RequestMapping(value = "cateInfo", method = "get")
    protected Object cateInfo(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        Map<String, String> params = this.getParameterMap(request);
        ResultEntity entity = (ResultEntity) netPlugin.doInvoke(request, response,
                params);// PortalPubFactory.getInstance().getPlugin("net")
        //.doInvoke(request, response, params);
        if (null == entity.getEntity()) {
            log.info(entity.getMessage());
        }
        return entity.getEntity();
    }

    /**
     * 查询成交deal对象
     */
    @RequestMapping(value = "actInfo", method = "get")
    protected Object actInfo(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        Map<String, String> params = this.getParameterMap(request);
        params.put("doCode", "actInfo");
        ResultEntity entity = (ResultEntity) uctPlugin.doInvoke(request, response,
                params);// PortalPubFactory.getInstance().getPlugin("uct")
        //.doInvoke(request, response, params);
        if (null == entity.getEntity()) {
            log.info(entity.getMessage());
        }
        return entity.getEntity();
    }

    /**
     * 查询成交deal对象
     */
    @RequestMapping(value = "roleInfo", method = "get")
    protected Object roleInfo(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        Map<String, String> params = this.getParameterMap(request);
        params.put("doCode", "roleInfo");
        ResultEntity entity = (ResultEntity) orgPlugin.doInvoke(request, response,
                params);//  PortalPubFactory.getInstance().getPlugin("org")
        //.doInvoke(request, response, params);
        if (null == entity.getEntity()) {
            log.info(entity.getMessage());
        }
        return entity.getEntity();
    }

    /**
     * 查询成交deal对象
     */
    @RequestMapping(value = "sysInfo", method = "get")
    protected Object sysInfo(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        Map<String, String> params = this.getParameterMap(request);
        params.put("doCode", "sysInfo");
        ResultEntity entity = (ResultEntity) orgPlugin.doInvoke(request, response,
                params);// PortalPubFactory.getInstance().getPlugin("org")
        //.doInvoke(request, response, params);
        if (null == entity.getEntity()) {
            log.info(entity.getMessage());
        }
        return entity.getEntity();
    }

    //////////////////以下对日志的处理doCode:logExecute////////////////////
    @RequestMapping(value = "logExecute", method = "get")
    //	@Token(remove = true)
    protected Object logExecute(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        //exeType=modelEntity/modelDetailEntity/modelDetailList
        Map<String, String> params = this.getParameterMap(request);
        String exeType = params.get("exeType");
        ResultEntity entity = (ResultEntity) uctPlugin.doInvoke(request, response, params);
        if ("modelDetailList".equalsIgnoreCase(exeType)) {
            Map<String, Object> data = new HashMap<String, Object>();
            data.put("flag", "T");
            if (entity.getEntity() instanceof Page) {
                Page page = (Page) entity.getEntity();
                data.put("total", page.getCount());
                data.put("rows", page.getPageObjects());
            } else {
                List list = (List) entity.getEntity();
                data.put("total", list.size());
                data.put("rows", list);
            }
            return data;
        } else {
            return entity.getEntity();
        }
    }
}
